package com.fingard.dsp.bank.directbank.fngrd01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAuthorizeForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.*;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.ReqAuthForRecDetail;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.RetAtsQryAuthForRecDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class FnGrd01AuthorizeForRec extends FnGrd01Base implements IAuthorizeForRec {
    @Override
    public RetAtsAuthForRec d_auth(ReqAuthForRec p_req) throws Exception {
        RetAtsAuthForRec tmpRetAts = new RetAtsAuthForRec(p_req.reqHeader);
        try {
            FnGrd01Header bankHeader = new FnGrd01Header();
            bankHeader.CorpBankCode = getBankConfig().getMapValueByKey("corBankCodeMapping", p_req.ownAct);
            if (StringHelper.isNullOrEmpty(bankHeader.CorpBankCode)) {
                WriteBankLogLn2("CorpBankCode为空，请配置corBankCodeMapping映射");
            }
            bankHeader.TransSeqID = getQryTransID();
            bankHeader.CommandCode = "312D0";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("BatchSignReq");
            xmlWriter.writeElementString("BatNo", p_req.reqSeqID);
            xmlWriter.writeElementString("TotalNum", String.valueOf(p_req.detailCount));//总笔数
            xmlWriter.writeElementString("CorpAct", p_req.ownAct);//企业账号
            xmlWriter.writeElementString("TransDate", Format.Date8Format.format(new Date()));//签名时间
            while (p_req.readRecord()) {
                ReqAuthForRecDetail tmpDetail = p_req.readedDetail;
                xmlWriter.writeStartElement("DetailRecord");
                xmlWriter.writeElementString("ProtocolCode", tmpDetail.authCode);//缴费编号
                xmlWriter.writeElementString("OppActName", tmpDetail.oppActName);//缴费客户姓名
                xmlWriter.writeElementString("OppAct", tmpDetail.oppAct);//缴费客户账号
                xmlWriter.writeElementString("CertType", tmpDetail.certType);//证件类型
                xmlWriter.writeElementString("CertNum", tmpDetail.certNum);//证件号码
                xmlWriter.writeElementString("Mobile", tmpDetail.cellPhone);//证件号码
                xmlWriter.writeElementString("PolicyNumber", StringHelper.hasAnyChar(tmpDetail.sourceNoteCode) ? tmpDetail.sourceNoteCode : tmpDetail.insuPolicyNum);//证件号码
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            xmlWriter.writeEndDocument();
            String[] tmpRetStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String state = tmpBkReader.getFirstText("/Root/BatchSignResp/State");
                    if ("2".equals(state)) {
                        tmpRetAts.respCode = "0";
                        tmpRetAts.respInfo = "上送成功";
                    } else if ("4".equals(state)) {
                        tmpRetAts.respCode = "0";
                        tmpRetAts.respInfo = "处理中";
                    } else if ("5".equals(state)) {
                        tmpRetAts.respCode = "-1";
                        tmpRetAts.respInfo = "批次号重复";
                    } else {
                        tmpRetAts.respCode = "-1";
                        tmpRetAts.respInfo = "上送失败";
                    }
                    tmpRetAts.retCode = tmpBkReader.getFirstText("/Root/BatchSignResp/InfoCode");
                    tmpRetAts.reqSeqID = p_req.reqSeqID;
                    tmpRetAts.retMsg = tmpBkReader.getFirstText("/Root/BatchSignResp/Info");
                } else {
                    tmpRetAts.respCode = "-1";
                    tmpRetAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = tmpRetStr[1];
            }
        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        tmpRetAts.writeRetToATS();
        return tmpRetAts;
    }

    @Override
    public RetAtsQryAuthForRec d_qry_auth(ReqQryAuthForRec p_req) throws Exception {
        RetAtsQryAuthForRec tmpRetAts = new RetAtsQryAuthForRec(p_req.reqHeader);
        tmpRetAts.writeStart();
        try {
            FnGrd01Header bankHeader = new FnGrd01Header();
            bankHeader.CorpBankCode = getBankConfig().getMapValueByKey("corBankCodeMapping", p_req.ownAct);
            if (StringHelper.isNullOrEmpty(bankHeader.CorpBankCode)) {
                WriteBankLogLn2("CorpBankCode为空，请配置corBankCodeMapping映射");
            }
            bankHeader.TransSeqID = getQryTransID();
            bankHeader.CommandCode = "212D0";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("BatchSignReq");
            xmlWriter.writeElementString("BatNo", p_req.reqSeqID);
            xmlWriter.writeElementString("TotalNum", p_req.totalNum);//总笔数
            xmlWriter.writeElementString("CorpAct", p_req.ownAct);//企业账号
            xmlWriter.writeElementString("TransDate", Format.Date8Format.format(new Date()));
            xmlWriter.writeEndDocument();
            xmlWriter.writeEndDocument();
            String[] tmpRetStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String state = tmpBkReader.getFirstText("/Root/BatchSignResp/State");
                    String tmpRetCode = tmpBkReader.getFirstText("/Root/BatchSignResp/InfoCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/Root/BatchSignResp/Info");
                    if ("2".equals(state)) {
                        while (tmpBkReader.read("/Root/BatchSignResp/DetailRecord")) {
                            RetAtsQryAuthForRecDetail tmpDetail = new RetAtsQryAuthForRecDetail();
                            XElement tmpRd = tmpBkReader.readedXElement;
                            tmpDetail.iSeqno = tmpRd.getSingleText("ProtocolCode");
                            tmpDetail.payInfo = tmpRd.getSingleText("SignInfo");
                            tmpDetail.newAuthCode = tmpRd.getSingleText("BankProtocolCode");
                            tmpDetail.authCode = tmpRd.getSingleText("ProtocolCode");
                            tmpDetail.oppAct = tmpRd.getSingleText("OppAct");
                            tmpDetail.oppActName = tmpRd.getSingleText("OppActName");
                            String tmpResult = tmpRd.getSingleText("SignState");
                            if ("2".equals(tmpResult)) {
                                tmpDetail.transState = "2";
                            } else if ("3".equals(tmpResult)) {
                                tmpDetail.transState = "3";
                            } else {
                                tmpDetail.transState = "4";
                            }
                            tmpDetail.payCode = tmpRd.getSingleText("SignInfoCode");
                            tmpRetAts.writeRetDetail(tmpDetail);
                        }
                        tmpRetAts.respCode = "0";
                        tmpRetAts.respInfo = tmpRetMsg;
                    }
                    if ("4".equals(state)) {
                        tmpRetAts.respCode = "0";
                        tmpRetAts.respInfo = "批次处理中--" + tmpRetMsg;
                    } else {
                        tmpRetAts.respCode = "-1";
                        tmpRetAts.respInfo = tmpRetMsg;
                    }
                } else {
                    tmpRetAts.respCode = "-1";
                    tmpRetAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = tmpRetStr[1];
            }
        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        if (tmpRetAts.getWriteRdCount() <= 0) {
            tmpRetAts.writeRetHead();
        }
        tmpRetAts.writeAfterDetail();
        return tmpRetAts;
    }

    @Override
    public RetAtsQryRecAuthInfo d_qry_authInfo(ReqQryRecAuthInfo p_req) throws Exception {
        return null;
    }
}
